<template>
  <div class="container">
    <el-card>
      <div class="pageTools">
        <!-- 左侧 -->
        <div class="left">
          <el-form :inline="true" :model="obj" class="demo-form-inline">
            <el-form-item label="目录名称">
              <el-input v-model="obj.keyword" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="obj.state" placeholder="请选择">
                <el-option label="启用" value='1'></el-option>
                <el-option label="禁用" value='0'></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="onSubmit">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="right">
          <!-- 右侧 -->
          <el-button type="success" icon="el-icon-edit" @click="isShowAdd=true"
            >新增技巧</el-button
          >
        </div>
      </div>
      <el-alert
        class="el-icon-info"
        type="info"
        :closable="false">
        数据一共{{total}}条
      </el-alert>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column  type="index" label="序号" width="50"></el-table-column>
        <el-table-column  label="文章标题"  width="400">
           <template #default='{row}'>
             <span v-html="row.title" >
             </span>
             <a v-if='row.videoURL' class="el-icon-film" @click.prevent="showVideo(row.videoURL)"></a>

          </template>

        </el-table-column>
        <el-table-column prop="visits" label="阅读数"  width="200"></el-table-column>
        <el-table-column prop="username" label="录入人"  width="200"></el-table-column>
        <el-table-column prop="createTime" label="录入时间"  width="280">
          <template #default='{row}'>{{row.createTime | parseTimeByString}}</template>
        </el-table-column>
        <el-table-column prop="state" label="状态"  width="100">
          <template #default='{row}'>
            {{row.state | stateFilter}}
          </template>
        </el-table-column>
        <el-table-column  label="操作"  width="200">
          <template #default='{row}'>
            <el-button type="text" @click="preview(row.id)">预览</el-button>
            <el-button type="text" @click="forbidden(row)"> {{row.state ===0? '启用':'禁用'}}</el-button>
            <el-button type="text" :disabled='row.state===1' @click="change(row)">修改</el-button>
            <el-button type="text" :disabled='row.state===1' @click="del(row)">删除</el-button>
          </template>

        </el-table-column>
    </el-table>

    <div class="pagination">
       <el-pagination
       background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="obj.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="obj.pagesize"
        layout="  prev, pager, next, sizes,jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    </el-card>
    <!-- 预览 -->
    <articles-preview v-if='isShow' :isShow.sync='isShow' :id='id'/>
    <!-- 视频 -->
    <div class="videoPreview" v-if='videoURL'>
      <div class="close" @click="closeVideo()"><span class="el-icon-close"></span></div>
      <div class="box">
        <video ref='video' :src="videoURL" autoplay></video>
      </div>
    </div>
    <articles-add :isShowAdd.sync='isShowAdd' :data='data'></articles-add>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles.js'
export default {
  name: 'articles',
  data () {
    return {
      total: 0,
      formInline: {
        user: '',
        region: ''
      },
      tableData: [],
      obj: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      isShow: false,
      id: '',
      videoShow: false,
      videoURL: null,
      isShowAdd: false,
      data: {}

    }
  },
  created () {
    this.getArticles()
  },
  methods: {
    onSubmit () {
      this.getArticles()
    },
    async getArticles () {
      const { data: { counts, items } } = await list(this.obj)

      this.tableData = items
      console.log(items)
      this.total = counts
    },
    handleSizeChange (val) {
      this.obj.pagesize = val
      this.getArticles()
    },

    handleCurrentChange (val) {
      this.obj.page = val
      this.getArticles()
    },
    // 预览
    preview (id) {
      this.isShow = true
      this.id = id
    },
    // 禁用
    async forbidden (row) {
      const state = row.state === 0 ? 1 : 0
      row.state = state
      await changeState(row)
      this.getArticles()
    },
    // 删除
    async del (row) {
      await remove(row)
      this.getArticles()
    },
    // 清除
    clear () {
      this.obj = {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      }
    },
    // 视频
    showVideo (videoURL) {
      this.videoURL = videoURL
    },
    closeVideo () {
      this.videoURL = null
    },
    // 修改
    change (row) {
      this.isShowAdd = true
      this.data = row
    }
  }
}
</script>

<style scoped lang="scss">
.el-icon-film{
  color: blue;
  font-size: 18px;
}
.pageTools{
  display: flex;
  justify-content: space-between;
}
.videoPreview{
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.3);
  overflow: hidden;
  z-index:9999;
  .close{
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%,0);
    background: rgba(0,0,0,.4);
    box-shadow: 0 0 5px rgba(0,0,0,0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color:#fff;
    font-size: 20px;
  }
  .box{
    width:800px;
    height:600px;
    position: absolute;
    left:50%;
    top:50%;
    transform: translate(-50%,-50%);
    video{
      width: 100%;
      height: 100%;
    }
  }
}
.pagination{
    float: right;
  }
</style>
